class BlogController < ApplicationController
 
  def index
    list
    render :action => 'list'
  end

  def list
    @article_pages, @articles = paginate :articles, :per_page => 5, :order => "id DESC"
  end

  def show
    @article = Article.find(params[:id])
  end

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(params[:article])
    if @article.save
      flash[:notice] = 'Le nouvel article a été ajouté.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @article = Article.find(params[:id])
  end

  def update
    @article = Article.find(params[:id])
    if @article.update_attributes(params[:article])
      flash[:notice] = "L'article a été mis à jour."
      redirect_to :action => 'show', :id => @article
    else
      render :action => 'edit'
    end
  end

  def destroy
    Article.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def ajout_commentaire
    # le commentaire ne sera pas créé si il est vide
    unless (params[:commentaire][:texte]).empty?
      c = Article.find(params[:id]).commentaires.create(params[:commentaire])
      render :partial => "commentaire", :locals => { :commentaire => c } 
    else
      render :nothing => true
    end
  end

end
